package com.itqf.ipmsys.service.impl;

import com.itqf.ipmsys.dto.UserDTO;
import com.itqf.ipmsys.entity.User;
import com.itqf.ipmsys.mapper.UserMapper;
import com.itqf.ipmsys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author corn
 * @since 2021-09-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    /**
     * 查询用户总数
     *
     * @return
     */
    @Override
    public Integer findUserTotal() {
        Integer count = userMapper.selectCount(null);
        return count;
    }

    /**
     * 月用户增长量
     *
     * @param month
     * @return
     */
    @Override
    public Map<String, List> findIncrease(int month) {
        Map<String, List> map = new HashMap<>();
        //用户类型
        List<String> types = new ArrayList<>();
        types.add("新增用户");
        types.add("活跃用户");
        types.add("临时停车用户");
        map.put("types", types);

        //新增用户
        List<UserDTO> newAdd = userMapper.selectNewAddUser(month);

        //活跃用户0--长期固定用户
        List<UserDTO> activeUser = userMapper.selectActiveUser(month);

        //临时停车用户--
        List<UserDTO> temporaryUser = userMapper.selectTemporaryUser(month);
        //整合时间
        List<Integer> date = unionDayData(newAdd, activeUser, temporaryUser);
        //从统计集合中获取数量 时间不存在的则设置数量为 0
        List<Integer> newAddCount = fetchUserDTOCount(date, newAdd);
        List<Integer> activeUserCount = fetchUserDTOCount(date, activeUser);
        List<Integer> temporaryUserCount = fetchUserDTOCount(date, temporaryUser);

        map.put("date", date);
        map.put("newincrease", newAddCount);
        map.put("active", activeUserCount);
        map.put("temporary", temporaryUserCount);

        return map;
    }

    /**
     * 获取日期数据并排序
     * @param newAdd
     * @param activeUser
     * @param temporaryUser
     * @return
     */
    private List<Integer> unionDayData(List<UserDTO> newAdd,
                                       List<UserDTO> activeUser,
                                       List<UserDTO> temporaryUser) {
        //获取三个数据的日期
        Set<Integer> set = new HashSet<>();
        for (UserDTO newAddUserDTO : newAdd) {
            set.add(newAddUserDTO.getDay());
        }
        for (UserDTO activeUserDTO : activeUser) {
            set.add(activeUserDTO.getDay());
        }
        for (UserDTO temporaryUserDTO : temporaryUser) {
            set.add(temporaryUserDTO.getDay());
        }
        List<Integer> date = new ArrayList<>(set);
        Collections.sort(date);
        return date;
    }

    /**
     * 从统计集合中获取数量 不存在则设置 0
     *
     * @param date
     * @param userDTOList
     * @return
     */
    private List<Integer> fetchUserDTOCount(List<Integer> date, List<UserDTO> userDTOList) {
        List<Integer> userSize = new ArrayList<>(date.size());
        for (int i = 0; i < date.size(); i++) {
            int dayCount = 0;
            Integer day =date.get(i);
            for (UserDTO userDTO : userDTOList) {
                if (day == userDTO.getDay()) {
                    dayCount = userDTO.getCount();
                    break;
                }
            }
            userSize.add(dayCount);
        }
        return userSize;
    }

}
